package com.hengx.cellview;

import android.text.Editable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.inputmethod.BaseInputConnection;
import com.hengx.cellview.cell.Cell;
import com.hengx.cellview.cell.EditTextCell;

public class CellInputConnection extends BaseInputConnection {
    
    public CellView view;
    
    public CellInputConnection(CellView targetView, boolean fullEditor) {
        super(targetView, fullEditor);
        this.view = targetView;
    }
    
    @Override
    public boolean commitText(CharSequence text, int newCursorPosition) {
        //获取到输入的字符
        return view.commitText(text);
    }
    
    @Override
    public boolean sendKeyEvent(KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (event.getKeyCode()) {
            case KeyEvent.KEYCODE_DEL:
                // 删除按键
                view.commitDelete();
                break;
            case KeyEvent.KEYCODE_ENTER:
                // 回车按键
                view.commitText("\n");
                break;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                // 左方向键
                view.moveCursorLeft();
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                // 右方向键
                view.moveCursorRight();
                break;
            case KeyEvent.KEYCODE_DPAD_UP:
                // 上方向键
                view.moveCursorTop();
                break;
            case KeyEvent.KEYCODE_DPAD_DOWN:
                // 下方向键
                view.moveCursorBottom();
                break;
            }
        }
        return true;
    }
    
    @Override
    public Editable getEditable() {
        Cell cell = view.getFocusCell();
        if (cell != null && cell instanceof EditTextCell) {
            return ((EditTextCell)cell).getEditable();
        }
        return null;
    }
    
    
    @Override
    public boolean finishComposingText() {
        return view.finishComposingText();
    }
    
}
